package com.tiancheng.trade.merchant.util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**   
 * @ClassName:  SHAUtil   
 * @Description: SHA256签名工具类
 */
public class SHAUtil {
	
	public static final String KEY_ALGORITHM = "SHA-256"; 
	public static final String ENCODE = "UTF-8";
	
	/**
	 * 
	 * @Title: sign
	 * @Description: sha-256签名
	 */
	public static String sign(String str) throws NoSuchAlgorithmException,
			UnsupportedEncodingException {
		MessageDigest messageDigest;
		messageDigest = MessageDigest.getInstance(KEY_ALGORITHM);
		messageDigest.update(str.getBytes(ENCODE));
		return CommonUtil.toHexString(messageDigest.digest());
	}
	
	/**
	 * @Description: 验证签名
	 */
	public static boolean verifySign(String str, String sign) throws Exception {
		boolean flag = false;
		String tmpSign = sign(str.trim());
		flag = tmpSign.equals(sign.trim());
		return flag;
	}
}
